//
//  EaseViewController.m
//  Education
//
//  Created by HUA on 15/7/14.
//  Copyright (c) 2015年 HUA. All rights reserved.
//

#import "EaseViewController.h"

@interface EaseViewController ()


//添加开始图片的imageview
@property(nonatomic,strong)UIImageView *mouseimageview;

@property(nonatomic,strong)UILabel *startLable;


//定义Lable和textfild
//被除数
@property(nonatomic,strong)UILabel *dividendNum;

//除数
@property(nonatomic,strong)UILabel *divisorNum;

//运算符
@property(nonatomic,strong)UILabel *operatorLable;

//等于号
@property(nonatomic,strong)UILabel *equalLable;

//结果
@property(nonatomic,strong)UITextField *resultsfild;







@end

@implementation EaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *bagroundimage = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bagroundimage.image = [UIImage imageNamed:@"35458PICec7.jpg"];
    bagroundimage.alpha = 0.6;
    bagroundimage.userInteractionEnabled  = YES;
    [self.view addSubview:bagroundimage];
    
    //添加一个返回按钮
    UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    backbutton.frame = CGRectMake(10, 20, 50, 30);
    [backbutton setTitle:@"返回" forState:UIControlStateNormal];
    [backbutton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backbuttonAcrion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backbutton];
    
    
    self.mouseimageview = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth/4, kScreenHeight/4, kScreenWidth/2, kScreenWidth/2)];
    
    
    self.mouseimageview.alpha = 1;
    self.mouseimageview.userInteractionEnabled = YES;
    self.mouseimageview.image = [UIImage imageNamed:@"11637863_164210506000_2.jpg"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired  = 1;
    [self.mouseimageview addGestureRecognizer:tap];
    [self.view addSubview:self.mouseimageview];
    
    self.startLable = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/4, kScreenHeight/4 +kScreenWidth/2 + 10, kScreenWidth/2, 30)];
    self.startLable.text = @"点击开始";
    self.startLable.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    tapp.numberOfTapsRequired = 1;
    tapp.numberOfTouchesRequired  = 1;
    [self.startLable addGestureRecognizer:tapp];
    self.startLable.font = [UIFont boldSystemFontOfSize:30];
    [self.startLable setTextColor:[UIColor brownColor]];
    self.startLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.startLable];
    
}

//开始图片手势触发方法
- (void)tapAction
{
    [UIView animateWithDuration:2 animations:^{
        
        self.mouseimageview.alpha = 0;
        self.startLable.alpha = 0;
        self.mouseimageview.userInteractionEnabled = NO;
        
    } completion:^(BOOL finished) {
        
        [self animationtimerAction];
        [self animationAppear];
    }];

}



//添加控件及动画
- (void)animationtimerAction
{
    
    self.dividendNum = [[UILabel alloc] initWithFrame:CGRectMake(10, (kScreenHeight - 30)/2 - 60, (kScreenWidth - 70)/5 + 10, 30)];
    self.dividendNum.backgroundColor = [UIColor redColor];
    self.dividendNum.alpha = 0;
    
    
    self.operatorLable = [[UILabel alloc] initWithFrame:CGRectMake(self.dividendNum.frame.origin.x + (kScreenWidth - 70)/5 + 20, (kScreenHeight - 30)/2 - 60, (kScreenWidth - 70)/5 - 20, 30)];
    self.operatorLable.backgroundColor = [UIColor redColor];
    self.operatorLable.alpha = 0;
    
    self.divisorNum = [[UILabel alloc] initWithFrame:CGRectMake(self.operatorLable.frame.origin.x + (kScreenWidth - 70)/5 - 10, (kScreenHeight - 30)/2 - 60, (kScreenWidth - 70)/5 + 10, 30)];
    self.divisorNum.backgroundColor = [UIColor redColor];
    self.divisorNum.alpha = 0;
    //等于号
    self.equalLable = [[UILabel alloc] initWithFrame:CGRectMake(self.divisorNum.frame.origin.x + (kScreenWidth - 70)/5 + 20, (kScreenHeight - 30)/2 - 60, (kScreenWidth - 70)/5 - 20, 30)];
    self.equalLable.backgroundColor = [UIColor redColor];
    self.equalLable.alpha = 0;
    
    self.resultsfild = [[UITextField alloc] initWithFrame:CGRectMake(self.equalLable.frame.origin.x + (kScreenWidth - 70)/5 - 10, (kScreenHeight - 30)/2 - 60, (kScreenWidth - 70)/5 + 30, 30)];
    self.resultsfild.borderStyle = UITextBorderStyleRoundedRect;
    self.resultsfild.backgroundColor = [UIColor redColor];
    self.resultsfild.alpha = 0;

    [self.view addSubview:self.resultsfild];
    [self.view addSubview:self.dividendNum];
    [self.view addSubview:self.operatorLable];
    [self.view addSubview:self.equalLable];
    [self.view addSubview:self.divisorNum];
 
}

//各种Lable显示的动画
- (void)animationAppear
{
    [UIView animateWithDuration:2 animations:^{
        
        self.resultsfild.alpha = 0.8;
        self.dividendNum.alpha = 0.8;
        self.dividendNum.text = [NSString stringWithFormat:@"%lu",[self arcrandom:30 num2:0]];
        self.operatorLable.alpha = 0.8;
        self.equalLable.alpha = 0.8;
        self.divisorNum.alpha = 0.8;
        
    } completion:^(BOOL finished) {
        
        
    }];
}

- (void)animationdisappear
{

    [UIView animateWithDuration:2 animations:^{
        
        self.resultsfild.alpha = 0;
        self.dividendNum.alpha = 0;
        self.operatorLable.alpha = 0;
        self.equalLable.alpha = 0;
        self.divisorNum.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        
    }];

}











//产生随机数的方法
- (NSInteger)arcrandom:(NSInteger)num1 num2:(NSInteger)num2
{


    return arc4random()%(num1 - num2) + num2;
}





//返回按钮的方法
- (void)backbuttonAcrion
{

    [self dismissViewControllerAnimated:YES completion:nil];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
